package com.example.mylitepaldemo.composeui.navig

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.mylitepaldemo.composeui.components.utils.showToast

@Composable
fun Page1(onclicked:() -> Unit) {
    Text(text = "1", Modifier.clickable {
        onclicked()
    }.padding(start = 100.dp))
}
@Composable
fun Page2(onclicked:() -> Unit,onback:() -> Unit) {
    Text(text = "2", Modifier.clickable { onclicked() }.padding(start = 200.dp))
    Button(onClick = { onback() }) { Text(text = "回退") }
}

@Composable
fun SwitchRegion(
    navController: NavHostController = rememberNavController(),//提供就认实现(如果外部没有调用跳转就不用传了)
    startDestination: String = "1",
modifier: Modifier = Modifier
) {
    NavHost(
        navController = navController,
        startDestination = startDestination,
        modifier = modifier
    )
    {
        composable(route = "1") { Page1(onclicked = {

            navController.navigate("2")
            }
        )
        }
        composable(route = "2") { Page2(onclicked = { navController.navigate("3") },onback ={ navController.navigateUp()} ) }
        composable(route = "3") {
            PageThree(onclicked = { navController.navigate("1") })

        }
    }

}
@Composable
fun PageThree(onclicked:() -> Unit) {
    Column(modifier = Modifier.background(Color.Red).fillMaxSize().wrapContentSize(align = Alignment.Center)) {
        Text(text = "Page Three", fontSize = 100.sp, color = Color.White)
        Button(onClick = { onclicked() }) { Text(text = "界面3跳转到界面1") }
//子界面中也有跳转时，将逻辑抛给外部
    }
}

